﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace KeyboardImageViewer
{
    public static class QueryOperations
    {
        private static int currentListId;

        public static int CurrentListId
        {
            get
            {
                return currentListId;
            }
        }

        public static void ChangeOrder()
        {
            if (ImageQuery.Sort == SortType.Modified)
            {
                ImageQuery.Sort = SortType.Random;
            }
            else if (ImageQuery.Sort == SortType.Random)
            {
                ImageQuery.Sort = SortType.Name;
                ImageQuery.Ascending = true;
            }
            else
            {
                ImageQuery.Sort = SortType.Modified;
                ImageQuery.Ascending = false;
            }

            ImageListControl.RunImageQuery(0);
        }

        public static void SetRatingView(int rating)
        {
            ImageQuery.MinRating = rating;

            ImageListControl.RunImageQuery(0);
        }

        public static void ToggleUntagged()
        {
            ImageQuery.UntaggedOnly = !ImageQuery.UntaggedOnly;

            ImageListControl.RunImageQuery(0);
        }

        public static void RunSearch(string searchTerm, bool searchFileName, bool searchTags)
        {
            ImageManager.Config.SearchFileName = searchFileName;
            ImageManager.Config.SearchTags = searchTags;

            ImageQuery.SearchFileName = searchFileName;
            ImageQuery.SearchTags = searchTags;
            ImageQuery.Search = searchTerm;

            ImageListControl.RunImageQuery(0);
        }

        public static void ClearSearch()
        {
            ImageQuery.Search = "";
            ImageQuery.CustomClause = "";

            ImageListControl.RunImageQuery(0);
        }

        public static void ShowImageList(int listId)
        {
            SetImageList(listId);

            ImageListControl.RunImageQuery(0);
        }

        public static void SetImageList(int listId)
        {
            currentListId = listId;

            SQLiteCommand command = new SQLiteCommand("SELECT name, orderBy, ascending, rating, searchString, searchTags, searchFileName, untagged, customClause FROM viewLists WHERE id = " + listId, ImageManager.Connection);
            using (SQLiteDataReader reader = command.ExecuteReader())
            {
                reader.Read();

                string orderBy = reader.GetString("orderBy");

                switch (orderBy)
                {
                    case "random":
                        ImageQuery.Sort = SortType.Random;
                        break;
                    case "name":
                        ImageQuery.Sort = SortType.Name;
                        break;
                    case "lastWriteTime":
                        ImageQuery.Sort = SortType.Modified;
                        break;
                    default:
                        break;
                }

                ImageQuery.Ascending = reader.GetBoolean("ascending");
                ImageQuery.MinRating = reader.GetInt32("rating");
                ImageQuery.Search = reader.GetString("searchString");
                ImageQuery.SearchTags = reader.GetBoolean("searchTags");
                ImageQuery.SearchFileName = reader.GetBoolean("searchFileName");
                ImageQuery.UntaggedOnly = reader.GetBoolean("untagged");
                ImageQuery.CustomClause = reader.GetString("customClause");
                ImageQuery.ListName = reader.GetString("name");
            }
        }
    }
}
